package product

import (
    "gorm.io/gorm"
    "ginweb/pkg/database"
)

type ProductRepository interface {
    CreateProduct(product *Product) error
    GetProduct(id string) (*Product, error)
    UpdateProduct(product *Product) error
    DeleteProduct(id string) error
}

type ProductRepositoryImpl struct {
    db *gorm.DB
}

func NewProductRepository() *ProductRepositoryImpl {
    return &ProductRepositoryImpl{db: database.DB}
}

func (r *ProductRepositoryImpl) CreateProduct(product *Product) error {
    return r.db.Create(product).Error
}

func (r *ProductRepositoryImpl) GetProduct(id string) (*Product, error) {
    var product Product
    if err := r.db.First(&product, id).Error; err != nil {
        return nil, err
    }
    return &product, nil
}

func (r *ProductRepositoryImpl) UpdateProduct(product *Product) error {
    return r.db.Save(product).Error
}

func (r *ProductRepositoryImpl) DeleteProduct(id string) error {
    return r.db.Delete(&Product{}, id).Error
}